package com.buaadu.clock;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.Icon;

public class NeedleIconManger {
	
	public SecondNeedleIcon getSecondNeedleIcon() {
		secondNeedleIcon.setRadiusAndHeight(getRadius(),getIconHeight());
		return secondNeedleIcon;
	}

	public MinuteNeedleIcon getMinuteNeedleIcon() {
		minuteNeedleIcon.setRadiusAndHeight(getRadius(),getIconHeight());
		return minuteNeedleIcon;
	}

	public HourNeedleIcon getHourNeedleIcon() {
		hourNeedleIcon.setRadiusAndHeight(getRadius(),getIconHeight());
		return hourNeedleIcon;
	}

	private SecondNeedleIcon secondNeedleIcon;
	private MinuteNeedleIcon minuteNeedleIcon;
	private HourNeedleIcon hourNeedleIcon;
	
	private int MinIconHeight = 4;
	
	int radius;
	
	public NeedleIconManger(){
		
		secondNeedleIcon = new SecondNeedleIcon();
		minuteNeedleIcon = new MinuteNeedleIcon();
		hourNeedleIcon = new HourNeedleIcon();
	}
	
	public int getRadius(){
		return radius;
	}
	
	public void setRadius(int radius){
		this.radius = radius;
	}
	
	public int getIconHeight(){
		return (int)(radius*0.1) > MinIconHeight?(int)(radius*0.1):MinIconHeight;
	}
}

class SecondNeedleIcon implements Icon {
	
	private int width;
	private int height;
	
	private float SECOND_NEEDLE_LENGTH_RATIO = 0.9f;
	
	public SecondNeedleIcon(){

	}
	
	public void setRadiusAndHeight(int width,int height){
		this.width = width;
		this.height = height;
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(Color.BLACK);
		g2.drawLine(x, y, x + (int)(getIconWidth()*SECOND_NEEDLE_LENGTH_RATIO), y);
	}

	@Override
	public int getIconWidth() {
		return width;
	}

	@Override
	public int getIconHeight() {
		return height;
	}

}
class MinuteNeedleIcon implements Icon {
	
	private float MINUTE_NEEDLE_LENGTH_RATIO = 0.8f;
	private int width;
	private int height;
	
	public MinuteNeedleIcon(){

	}
	public void setRadiusAndHeight(int width,int height){
		this.width = width;
		this.height = height;
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		Graphics2D g2 = (Graphics2D) g;
		
		int[] xs = new int[4];
		int[] ys = new int[4];
		
		xs[0] = x;
		xs[1] = xs[3] = x + (int)(getIconWidth() * 0.1);
		xs[2] = x + (int)(getIconWidth() * MINUTE_NEEDLE_LENGTH_RATIO);
		
		ys[0] = ys[2] = y;
		ys[1] = y - (int)(getIconHeight() * 0.5);
		ys[3] = y + (int)(getIconHeight() * 0.5);
		
		for(int i = 0;i < 4;i++){
			ys[i] = ys[i] + 1;
		}
		
		g2.setColor(Color.GRAY);
		g2.fillPolygon(xs, ys, 4);
		
		for(int i = 0;i < 4;i++){
			ys[i] = ys[i] - 1;
		}
		
		g2.setColor(new Color(0,119,119));
		g2.fillPolygon(xs, ys, 4);
		

	}

	@Override
	public int getIconWidth() {
		return width;
	}

	@Override
	public int getIconHeight() {
		return height;
	}

}
class HourNeedleIcon implements Icon {
	
	private float HOUR_NEEDLE_LENGTH_RATIO = 0.5f;
	private int width;
	private int height;
	
	public HourNeedleIcon(){

	}
	
	public void setRadiusAndHeight(int width,int height){
		this.width = width;
		this.height = height;
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		Graphics2D g2 = (Graphics2D) g;
		
		int[] xs = new int[4];
		int[] ys = new int[4];
		
		xs[0] = x;
		xs[1] = xs[3] = x + (int)(getIconWidth() * 0.1);
		xs[2] = x + (int)(getIconWidth() * HOUR_NEEDLE_LENGTH_RATIO);
		
		ys[0] = ys[2] = y;
		ys[1] = y - (int)(getIconHeight() * 0.5);
		ys[3] = y + (int)(getIconHeight() * 0.5);
		
		for(int i = 0;i < 4;i++){
			ys[i] = ys[i] + 1;
		}
		
		g2.setColor(Color.GRAY);
		g2.fillPolygon(xs, ys, 4);
		
		for(int i = 0;i < 4;i++){
			ys[i] = ys[i] - 1;
		}
		
		g2.setColor(new Color(0,119,119));
		g2.fillPolygon(xs, ys, 4);
		

	}

	@Override
	public int getIconWidth() {
		return width;
	}

	@Override
	public int getIconHeight() {
		return height;
	}

}
